package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dto.HomeShowDTO;
import io.aircraft.modules.wms.entity.HomeShowEntity;
import io.aircraft.modules.wms.entity.LocationEntity;
import io.aircraft.modules.wms.service.LocationService;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.modules.wms.dao.LocationDao;
import io.aircraft.modules.wms.dto.LocationDTO;
import io.aircraft.modules.wms.service.StockService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 货位信息
 *
 * @author Cycle cycle@gmail.com
 * @since 1.0.0 2022-10-07
 */
@Service
public class LocationServiceImpl extends CrudServiceImpl<LocationDao, LocationEntity, LocationDTO> implements LocationService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private StockService stockService;

    @Override
    public PageData<LocationDTO> page(Map<String, Object> params) {
        IPage<LocationEntity> page = baseDao.selectPage(
                getPage(params, "locate_id", true),
                getWrapper(params)
        );
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, LocationDTO.class);
    }

    @Override
    public QueryWrapper<LocationEntity> getWrapper(Map<String, Object> params) {
        String locateNo = (String) params.get("locateNo");
        String dvcNo = (String) params.get("dvcNo");
        String locateType = (String) params.get("locateType");
        String rowNum = (String) params.get("rowNum");
        String colNum = (String) params.get("colNum");
        String levelNum = (String) params.get("levelNum");
        String contNo = (String) params.get("contNo");
        String locateSt = (String) params.get("locateSt");
        String active = (String) params.get("active");

        QueryWrapper<LocationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(dvcNo), "dvc_no", dvcNo);
        wrapper.eq(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(locateType), "locate_type", locateType);
        wrapper.eq(StringUtils.isNotBlank(rowNum), "row_num", rowNum);
        wrapper.eq(StringUtils.isNotBlank(colNum), "col_num", colNum);
        wrapper.eq(StringUtils.isNotBlank(levelNum), "level_num", levelNum);
        wrapper.eq(StringUtils.isNotBlank(locateSt), "locate_st", locateSt);
        wrapper.eq(StringUtils.isNotBlank(active), "active", active);

        wrapper.like(StringUtils.isNotBlank(locateNo), "locate_no", locateNo);

        return wrapper;
    }

    @Override
    public void delete(Long[] ids) {
        super.delete(ids);
    }

    @Override
    public HomeShowDTO getHomeShowDetails(Map<String, Object> params) {
        //1.获取货位的使用详情
        HomeShowEntity lcEntity = baseDao.selectLocationDetails();
        HomeShowDTO showDTO = ConvertUtils.sourceToTarget(lcEntity, HomeShowDTO.class);
        //2.获取库存的详情信息(group by spu)
        showDTO.setStockDetailList(stockService.getStockDetailsGroupBySpu());
        return showDTO;
    }
}